﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.ServiceModel;
using System.Web.Security;
using System.Runtime.Serialization;
using System.Web.Profile;
//using System.Threading;

namespace WcfService
{
    public partial interface IService
    {

        [OperationContract]
        DataSet aspnet_Users_GetAll();

        [OperationContract]
        bool aspnet_Users_Delete(string UserName);

        [OperationContract]
        bool aspnet_Users_ChangePassword(string userName, string oldPass, string passWord);

        [OperationContract]
        bool aspnet_Users_IsExist(string userName);

        [OperationContract]
        string aspnet_Users_Create(string UserName, string PassWord, string Email, bool isAdmin, bool viewAllCompany, bool viewAllBranch);

        [OperationContract]
        string aspnet_Users_Update(string UserName, string PassWord, string Email, bool isAdmin, bool viewAllCompany, bool viewAllBranch);

        [OperationContract]
        DataSet aspnet_Users_GetInfo(string userName);

    }

    public partial class Service : IService
    {
        public DataSet aspnet_Users_GetAll()
        {
            DataSet ds = new DataSet();
            MembershipUserCollection userCollection = Membership.GetAllUsers();
            DataTable dt = new DataTable("Users");
            dt.Columns.Add("UserName", typeof(String));
            dt.Columns.Add("Email", typeof(String));

            dt.Columns.Add("isAdmin", typeof(String));
            dt.Columns.Add("viewAllCompany", typeof(String));
            dt.Columns.Add("viewAllBranch", typeof(String));


            foreach (MembershipUser _user in userCollection)
            {
                ProfileBase settings = ProfileBase.Create(_user.UserName);
                string isAdmin = (string)settings["isAdmin"];
                string viewAllCompany = (string)settings["viewAllCompany"];
                string viewAllBranch = (string)settings["viewAllBranch"];

                dt.Rows.Add(new String[] { _user.UserName, 
                                           _user.Email,
                                           isAdmin,
                                           viewAllCompany,
                                           viewAllBranch
                                          }
                            );
            }
            ds.Tables.Add(dt);
            return ds;
        }

        public bool aspnet_Users_Delete(string UserName)
        {
            return Membership.DeleteUser(UserName, true);
        }


        public bool aspnet_Users_ChangePassword(string userName, string oldPass, string passWord)
        {
            return (Membership.GetUser(userName).ChangePassword(oldPass, passWord));
        }

        public bool aspnet_Users_IsExist(string userName)
        {
            return Membership.FindUsersByName(userName).Count > 0 ? true : false;
        }

        public string aspnet_Users_Create(string UserName, string PassWord, string Email, bool isAdmin, bool viewAllCompany, bool viewAllBranch)
        {
            string retVal = "";
            try
            {

                MembershipCreateStatus status;

                Membership.CreateUser(UserName, PassWord, Email, UserName, UserName, true, out status);

                retVal = status.ToString();

                ProfileBase settings = ProfileBase.Create(UserName);
                settings["isAdmin"] = isAdmin.ToString();
                settings["viewAllCompany"] = viewAllCompany.ToString();
                settings["viewAllBranch"] = viewAllBranch.ToString();
                settings.Save();
            }
            catch (Exception ex)
            {
                retVal = ex.Message;
            }
            return retVal;
        }

        public string aspnet_Users_Update(string UserName, string PassWord, string Email, bool isAdmin, bool viewAllCompany, bool viewAllBranch)
        {
            string retVal = "";
            string oldPass;
            try
            {
                MembershipUser user = Membership.GetUser(UserName);
                oldPass = user.GetPassword();

                if (user.ChangePasswordQuestionAndAnswer(oldPass, UserName, UserName))
                {
                    if (user.ChangePassword(oldPass, PassWord))
                    {
                        user.Email = Email;
                        Membership.UpdateUser(user);

                        ProfileBase settings = ProfileBase.Create(UserName);
                        settings["isAdmin"] = isAdmin.ToString();
                        settings["viewAllCompany"] = viewAllCompany.ToString();
                        settings["viewAllBranch"] = viewAllBranch.ToString();
                        settings.Save();

                    }
                    else
                        retVal = "False";
                }
                else
                    retVal = "False";
            }
            catch (Exception ex)
            {
                retVal = ex.Message;
            }

            return retVal;
        }

        public DataSet aspnet_Users_GetInfo(string userName)
        {
            MembershipUser user = Membership.GetUser(userName);
            DataTable dataTable = new DataTable("Users");
            dataTable.Columns.Add("UserName", typeof(String));
            dataTable.Columns.Add("Password", typeof(String));
            dataTable.Columns.Add("Email", typeof(String));
            dataTable.Columns.Add("IsAnonymous", typeof(Boolean));

            dataTable.Columns.Add("isAdmin", typeof(Boolean));
            dataTable.Columns.Add("viewAllCompany", typeof(Boolean));
            dataTable.Columns.Add("viewAllBranch", typeof(Boolean));


            ProfileBase settings = ProfileBase.Create(userName);
            string isAdmin = Util.ToBoolean(settings["isAdmin"]).ToString();
            string viewAllCompany = Util.ToBoolean(settings["viewAllCompany"]).ToString();
            string viewAllBranch = Util.ToBoolean(settings["viewAllBranch"]).ToString();

            //Tìm retVal.IsAnonymous
            string IsAnonymous = SqlHelper.ExecuteScalar(Properties.Settings.Default.ConnectionString, "Select IsAnonymous from aspnet_Users where username = '" + userName + "'").ToString();

            dataTable.Rows.Add(new String[] { user.UserName, user.GetPassword(), user.Email, IsAnonymous, isAdmin, viewAllCompany, viewAllBranch });

            DataSet ds = new DataSet();
            ds.Tables.Add(dataTable);
            return ds;
        }


    }

}
